--[[
All the code appearing in the module originated in the oUF_Caellian project 
(Caellian):

http://www.wowinterface.com/downloads/info9974-oUF_Caellian.html
]]

local colors = setmetatable({
	happiness = setmetatable({
		[1] = {.69,.31,.31},
		[2] = {.65,.63,.35},
		[3] = {.33,.59,.33},
	}, {__index = oUF.colors.happiness}),
}, {__index = oUF.colors})

local function utf8sub(string, i, dots)
	local bytes = string:len()
	if bytes <= i then
		return string
	else
		local len, pos = 0, 1
		while pos <= bytes do
			len = len + 1
			local c = string:byte(pos)
			if c > 0 and c <= 127 then
				pos = pos + 1
			elseif c >= 194 and c <= 223 then
				pos = pos + 2
			elseif c >= 224 and c <= 239 then
				pos = pos + 3
			elseif c >= 240 and c <= 244 then
				pos = pos + 4
			end
			if len == i then break end
		end

		if len == i and pos <= bytes then
			return string:sub(1, pos - 1)..(dots and '...' or '')
		else
			return string
		end
	end
end

if not oUF.Tags['[colorfeed]'] then
	oUF.Tags['[colorfeed]'] = function(unit)
		local reaction = UnitReaction(unit, 'player')
		if unit == 'pet' and GetPetHappiness() then
			local c = colors.happiness[GetPetHappiness()]
			return string.format('|cff%02x%02x%02x', c[1] * 255, c[2] * 255, c[3] * 255)
		elseif UnitIsPlayer(unit) then
			return oUF.Tags['[raidcolor]'](unit)
		elseif reaction then
			local c = FACTION_BAR_COLORS[reaction]
			return string.format('|cff%02x%02x%02x', c.r * 255, c.g * 255, c.b * 255)
		else
			r, g, b = .84,.75,.65
			return string.format('|cff%02x%02x%02x', r * 255, g * 255, b * 255)
		end
	end
end

if not oUF.Tags['[diffcolor]'] then
	oUF.Tags['[diffcolor]']  = function(unit)
		local r, g, b
		local level = UnitLevel(unit)
		if level < 1 then
			r, g, b = .69,.31,.31
		else
			local diffcolor = UnitLevel('target') - UnitLevel('player')
			if diffcolor >= 5 then
				r, g, b = .69,.31,.31
			elseif diffcolor >= 3 then
				r, g, b = .71,.43,.27
			elseif diffcolor >= -2 then
				r, g, b = .84,.75,.65
			elseif -diffcolor <= GetQuestGreenRange() then
				r, g, b = .33,.59,.33
			else
				r, g, b = .55,.57,.61
			end
		end
		return string.format('|cff%02x%02x%02x', r * 255, g * 255, b * 255)
	end
end

if not oUF.Tags['[shortsmall]'] then
	oUF.Tags['[shortsmall]']  = function(unit)
		local name = UnitName(unit)
		return utf8sub(name, 6, false)
	end
end

if not oUF.Tags['[shortmedium]'] then
	oUF.Tags['[shortmedium]']  = function(unit)
		local name = UnitName(unit)
		if(unit == 'pet' and name == 'Unknown') then
			return 'Pet'
		else
			return utf8sub(name, 12, true)
		end
	end
end

if not oUF.Tags['[shortlarge]'] then
	oUF.Tags['[shortlarge]']  = function(unit)
		local name = UnitName(unit)
		return utf8sub(name, 18, true)
	end
end

oUF.TagEvents['[colorfeed]'] = 'UNIT_HAPPINESS'
oUF.TagEvents['[diffcolor]'] = 'UNIT_LEVEL'
oUF.TagEvents['[shortsmall]']   = 'UNIT_NAME_UPDATE'
oUF.TagEvents['[shortmedium]']   = 'UNIT_NAME_UPDATE'
oUF.TagEvents['[shortlarge]']   = 'UNIT_NAME_UPDATE'